﻿using LanguageControlBase.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FinalTestMachine.View.MainUnits
{
    /// <summary>
    /// Unit_SiteStationSetting.xaml 的交互逻辑
    /// </summary>
    public partial class Unit_SiteStationSetting : UserControl
    {
        public Unit_SiteStationSetting()
        {
            InitializeComponent();
            LoadUI();
        }
        public static List<LanCheckBox> lanCheckBoxes = new List<LanCheckBox>();
        void LoadUI()
        {
            for (int i = 0; i < GlobalParams.WorkstationSum; i++) {
                // 创建 LanCheckBox
                LanCheckBox firstCheckBox = new LanCheckBox
                {
                    Name = "cb_First",
                    LanContent = "启用工位" + (i+1) + "测试站",  // 或者使用 LanContent 属性，取决于 LanCheckBox 的实现
                    Margin = new Thickness(20, 5, 0, 0),
                    Width = Double.NaN,  // 相当于 XAML 中的 "Auto"
                    Height = 20,
                    VerticalAlignment = VerticalAlignment.Top
                };
                lanCheckBoxes.Add(firstCheckBox);
                // 添加点击事件处理
                int index = i;
                firstCheckBox.Click += (s,e) => {
                    try
                    {
                        if (this.IsInitialized == false) return;
                        string fieldName = "Site" + (index+1) + "IsUse";
                        GlobalParams.SetFieldValue(Funs.FunStationSettings.Cur, fieldName, firstCheckBox.IsChecked);
                        Funs.FunStationSettings.Cur.Save();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                };

                // 将 CheckBox 添加到 StackPanel
                panel_disableStation.Children.Add(firstCheckBox);
            }
        }

        private void SelectionChanged_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                if (this.IsInitialized == false) return;
                if(cb_MainChoice.SelectedIndex==0)
                {
                    Funs.FunStationSettings.Cur.IsMust = true;
                }
                else
                {
                    Funs.FunStationSettings.Cur.IsMust = false;
                }
                Funs.FunStationSettings.Cur.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (Funs.FunStationSettings.Cur.IsMust)
            {
                cb_MainChoice.SelectedIndex = 0;
            }
            else
            {
                cb_MainChoice.SelectedIndex = 1;
            }

            for (int i = 0; i < lanCheckBoxes.Count; i++) {
                int index = i;
                string fieldName = "Site" + (index + 1) + "IsUse";
                lanCheckBoxes[i].IsChecked = GlobalParams.GetFieldValue<bool>(Funs.FunStationSettings.Cur, fieldName);
            }
        }
    }
}
